
#include "QtStra3.h"
#include "QtStra3_InstTableDelegate.h"



QtStra3::QtStra3(QDockWidget* instDock, QDockWidget* strategyDock, QTabWidget::TabPosition pos, QWidget *parent): 
QtStraTemplate(3, instDock, strategyDock, pos, parent)
{
	m_instTable = new QTableWidget();
	m_instTable->setItemDelegate(new QtStra3InstTableDelegate);
	m_instTable->setSelectionBehavior(QAbstractItemView::SelectRows);
	m_instTable->setAlternatingRowColors(true);


	SetInstTable(m_instTable);

	InitInstTable();
}



QString QtStra3::DefaultInstTableValue(int col)
{
	if (col == 1)
	{
		return "1";
	}
	else if (col == 2 )
	{
		return StrategyUser4_CInstMap_NameSpace::CInstMap::OPEN.c_str();
	}
	else if (col == 3 )
	{
		return StrategyUser4_CInstMap_NameSpace::CInstMap::BUY.c_str();
	}
	else if (col == 4)
	{
		return "1";
	}
	else
		return "";
}


void QtStra3::SetUserdataNew()
{
	m_pstrauserdata->ClearNew();



	//get count
	int rowcnt = m_instTable->rowCount();
	int colcnt = m_instTable->columnCount();
	//
	//from every row
	for (int rowIndex = 0; rowIndex < rowcnt; rowIndex++)
	{
		Qt::CheckState c = m_instTable->item(rowIndex, 0)->checkState();

		//if checked
		if (Qt::Checked == c)
		{
			char inst[20] = {0}; int reqvol; char offset = 0; char direct = 0; double tradeprice = 0; 

			//from every column
			for (int colIndex = 0; colIndex < colcnt; colIndex++)
			{
				QTableWidgetItem* item =  m_instTable->item(rowIndex, colIndex);


				if (0 == colIndex)
					strcpy(inst, item->text().toStdString().c_str());		
				else if (1 == colIndex)
				{
					reqvol = item->text().toInt();
				}
				else if (2 == colIndex)
				{
					if (item->text().toStdString() == StrategyUser4_CInstMap_NameSpace::CInstMap::OPEN) 
						offset = THOST_FTDC_OF_Open;
					else
						offset = THOST_FTDC_OF_CloseToday;
				}
				else if (3 == colIndex)
				{
					if (item->text().toStdString() == StrategyUser4_CInstMap_NameSpace::CInstMap::BUY) 
						direct = THOST_FTDC_D_Buy;
					else
						direct = THOST_FTDC_D_Sell;
				}	
				else if (4 == colIndex)
				{
					tradeprice = item->text().toDouble();
				}	
					
			}



			QtStraUserdataCore<userdata_stra3>::QtStraData barrlt;
			{
				BarInfo& bar = barrlt.bar;
				strcpy(bar.instru, inst);
				bar.type = tick;
				bar.cnt = 1;
				//barrlt.timertype = timertype;
				userdata_stra3& userdata = barrlt.userdata;


				userdata.reqvol = reqvol;
				userdata.offset = offset;
				userdata.direct = direct;
				userdata.tradeprice = tradeprice;
			}




			//add new
			m_pstrauserdata->AddNew(barrlt);

		}

	}
}

